using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using extend_360.dto;

namespace extend_360.utils
{
    public class FileUtils
    {
        /**
         * 读取JSON文件返回实体
         */
        public static T ReadFile<T>(string path) where T : class, new()
        {
            if (!File.Exists(path))
            {
                SystemUtils.ConsoleWrite($"文件:{path}不存在", ConsoleColor.Red);
                return default;
            }

            string content = File.ReadAllText(path);
            return JsonSerializer.Deserialize<T>(content);
        }

        /**
         * 读取文件夹下所有文件
         */
        public static List<String> GetFiles(string directoryPath)
        {
            if (!Directory.Exists(directoryPath))
            {
                SystemUtils.ConsoleWrite($"文件夹:{directoryPath}不存在", ConsoleColor.Red);
                return default;
            }

            return Directory.GetFiles(directoryPath).ToList();
        }

        /// <summary>
        /// 获取文件夹下所有目录 返回文件信息
        /// </summary>
        /// <param name="filePaths"></param>
        /// <returns></returns>
        public static List<FileInfoDto> GetFileInfo(List<string> filePaths)
        {
            List<FileInfoDto> fileInfoDtos = new List<FileInfoDto>();
            if (filePaths == null || filePaths.Count == 0)
                return fileInfoDtos;
            foreach (var path in filePaths)
            {
                if (!File.Exists(path))
                    continue;
                FileInfo fileInfo = new FileInfo(path);
                FileInfoDto fileInfoDto = new FileInfoDto()
                {
                    FileName = fileInfo.Name,
                    FilePath = fileInfo.FullName
                };
                fileInfoDtos.Add(fileInfoDto);
            }

            return fileInfoDtos;
        }

        /**
         * 读取文本多行内容
         */
        public static IEnumerable<string> ReadFiles(string path)
        {
            if (!File.Exists(path))
            {
                SystemUtils.ConsoleWrite($"文件:{path}不存在", ConsoleColor.Red);
                return default;
            }

            return File.ReadLines(path);
        }

        /**
         * 随机读文件夹下的文件 并读取文本内容中的一行数据
         */
        public static string ReadFile(string directoryPath)
        {
            try
            {
                if (Directory.Exists(directoryPath))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
                    FileInfo[] fileInfos = directoryInfo.GetFiles();
                    if (fileInfos.Length == 0)
                        return "如何辨别正品化妆品";
                    FileInfo fileInfo = fileInfos[new Random().Next(0, fileInfos.Length - 1)];
                    IEnumerable<string> contents = ReadFiles(fileInfo.FullName);
                    if (contents != null)
                    {
                        int count = new Random().Next(0, contents.Count() - 1);
                        return contents.ToList()[count];
                    }
                }
            }
            catch (Exception e)
            {
                SystemUtils.ConsoleWrite($"读取文件{directoryPath}失败，" + e.Message, ConsoleColor.Red);
            }

            return "如何辨别正品化妆品";
        }
    }
}